<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="分类名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="关键字" prop="keywords">
      <el-input v-model="dataForm.keywords" placeholder="关键字"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="frontDesc">
      <el-input v-model="dataForm.frontDesc" type="textarea" :rows="3" placeholder="描述"></el-input>
    </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-radio v-model="dataForm.level" label="L1">L1</el-radio>
        <el-radio v-model="dataForm.level" label="L2">L2</el-radio>
      </el-form-item>
    <el-form-item v-if="dataForm.level === 'L2'" label="父节点" prop="parentId">
      <el-select v-model="dataForm.parentId" placeholder="请选择">
        <el-option
          v-for="item in categoryList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="排序" prop="sortOrder">
      <el-input-number v-model="dataForm.sortOrder" controls-position="right" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="首页展示排序" prop="showIndex">
      <el-input-number v-model="dataForm.showIndex" controls-position="right" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="显示" prop="isShow">
      <el-radio v-model="dataForm.isShow" label="1">是</el-radio>
      <el-radio v-model="dataForm.isShow" label="0">否</el-radio>
    </el-form-item>
    <el-form-item label="banner图片" prop="bannerUrl">
      <el-input v-model="dataForm.bannerUrl" placeholder="banner图片"></el-input>
    </el-form-item>
    <el-form-item label="icon链接" prop="iconUrl">
      <el-input v-model="dataForm.iconUrl" placeholder="icon链接"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="imgUrl">
      <el-input v-model="dataForm.imgUrl" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="手机banner" prop="wapBannerUrl">
      <el-input v-model="dataForm.wapBannerUrl" placeholder="手机banner"></el-input>
    </el-form-item>
    <el-form-item label="frontName" prop="frontName">
      <el-input v-model="dataForm.frontName" type="textarea" :rows="2" placeholder="frontName"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          keywords: '',
          frontDesc: '',
          parentId: '',
          sortOrder: '',
          showIndex: '',
          isShow: '1',
          bannerUrl: '',
          iconUrl: '',
          imgUrl: '',
          wapBannerUrl: '',
          level: 'L1',
          type: '',
          frontName: ''
        },
        categoryList: [],
        dataRule: {
          name: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this.getParentCategory()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/category/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.keywords = data.data.keywords
                this.dataForm.frontDesc = data.data.frontDesc
                this.dataForm.parentId = data.data.parentId
                this.dataForm.sortOrder = data.data.sortOrder
                this.dataForm.showIndex = data.data.showIndex
                this.dataForm.isShow = data.data.isShow
                this.dataForm.bannerUrl = data.data.bannerUrl
                this.dataForm.iconUrl = data.data.iconUrl
                this.dataForm.imgUrl = data.data.imgUrl
                this.dataForm.wapBannerUrl = data.data.wapBannerUrl
                this.dataForm.level = data.data.level
                this.dataForm.type = data.data.type
                this.dataForm.frontName = data.data.frontName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/category/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'keywords': this.dataForm.keywords,
                'frontDesc': this.dataForm.frontDesc,
                'parentId': this.dataForm.parentId,
                'sortOrder': this.dataForm.sortOrder,
                'showIndex': this.dataForm.showIndex,
                'isShow': this.dataForm.isShow,
                'bannerUrl': this.dataForm.bannerUrl,
                'iconUrl': this.dataForm.iconUrl,
                'imgUrl': this.dataForm.imgUrl,
                'wapBannerUrl': this.dataForm.wapBannerUrl,
                'level': this.dataForm.level,
                'type': this.dataForm.type,
                'frontName': this.dataForm.frontName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getParentCategory () {
        this.$http({
          url: this.$http.adornUrl('/platform/category/getCategorySelect'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.categoryList = data.list
        })
      }
    }
  }
</script>
